package tbdp.tool.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;

public class GsonUtils {

    private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();

    /**
     * json to map
     */
    public static Map<String, String> toMap(String content) {
        Type type = new TypeToken<Map<String, String>>() {
        }.getType();
        Map<String, String> rs = gson.fromJson(content, type);
        return rs;
    }

    /**
     * json to object
     *
     * @param jsonString ,class
     */
    public static <T> T fromJson(String jsonString, Type type) {
        return gson.fromJson(jsonString, type);
    }

    /**
     * object to json
     */
    public static String toJson(Object jsonObject) {
        return gson.toJson(jsonObject);
    }

}